<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'appinclude.php';
require_once 'funcinclude.php';

// numeric

$form_country = $_REQUEST['country'];
$form_district = $_REQUEST['district'];
$form_groupid = $_REQUEST['groupid'];

settype($form_country, "integer");
settype($form_district, "integer");
settype($form_groupid, "float");


// strings
function CleanName($n) {
	$n=explode(' ', strtolower(trim($n)));
	$r=array(strtolower($GLOBALS[_ClubName]), 'club');
	$n=implode(' ', array_diff($n, $r));
	return $n;
}
function CapitalizeName($name) {
	$name = strtolower($name);
	$name = join("'", array_map('ucwords', explode("'", $name)));
	$name = join("-", array_map('ucwords', explode("-", $name)));
	return $name;
}

$form_club = CapitalizeName(CleanName($_REQUEST['club']));
$form_city = CapitalizeName(trim($_REQUEST['city']));
$form_url = trim($_REQUEST['url']);

if(get_magic_quotes_gpc()) {
	$form_club = stripslashes($form_club);
	$form_city = stripslashes($form_city);
	$form_url = stripslashes($form_url);
}

$link = mysql_connect($_mysql_server, $_mysql_user, $_mysql_password) or die();
mysql_select_db($_mysql_db) or die();

if (!empty($form_club) && !empty($form_city) && $form_country!=0 && $form_district!=0) {
	
	if ($form_url!='' AND stripos($form_url, 'http://')===false AND stripos($form_url, 'https://')===false) { $form_url='http://'.$form_url; }
	
	// add club to db
	$query = sprintf("INSERT INTO clubs (id, name, district, country, city, url, fb_groupid, userid, verified, ts) VALUES (NULL, '%s', %d, %d, '%s', '%s', %d, %d, 0, NOW())",
		mysql_real_escape_string($form_club, $link),
		$form_district,
		$form_country,
		mysql_real_escape_string($form_city, $link),
		mysql_real_escape_string($form_url, $link),
		$form_groupid,
		$user);   
	$result = mysql_query($query) or die();
	$club_id_new = mysql_insert_id();
	
	// changes on user level
	if ($club_id_new!=0 && $club_id_new!=false) {
		$member = member($user);
		if ($member!=false) {
			$query = sprintf("UPDATE users SET club_id=%d, position=9, changes=%d WHERE users.fb_id=%d LIMIT 1",
				$club_id_new,
				security($member[changes]),
				$user);
			$result = mysql_query($query) or die();
		} else {
			$query = sprintf("INSERT INTO users (fb_id, club_id, verified, ts) VALUES (%d, %d, 1, NOW())",
				$user,
				$club_id_new);
			$result = mysql_query($query) or die();
		}
		$member = member($user);
		
		// update profile 
		$facebook->api_client->profile_setFBML('', $user, profile($member), '', '');

		// publish feed
		$feed = feed('club', $member);
		$facebook->api_client->feed_publishTemplatizedAction($feed[title_template], $feed[title_data], $feed[body_template], $feed[body_data], '');
    }	
	
	mysql_close($link);
	echo '<fb:redirect url="'.$appcallbackurl.'?added_club=1">';
} else {
	// get groups for specific user
	$group_arr = $facebook->api_client->fql_query("SELECT gid, name, nid, description, group_type, group_subtype, recent_news, pic, pic_big, pic_small, creator, update_time, office, website, venue FROM group WHERE gid IN (SELECT gid FROM group_member WHERE uid='$user' AND gid!='2204778486')");
	if (!empty($group_arr)) {
		foreach ($group_arr as $key => $value) {
    		$groups[$value[gid]]=$value[name];
		}
	}
?>


<fb:header decoration="add_border">Add New <?php echo $_ClubName; ?> Club</fb:header>
<?php 
	if (isset($_REQUEST['add'])) {
		echo '<fb:error message="Missing Required Entry">';
		echo ((empty($form_club)) ? $_ClubName.' Club Name<br>' : '' );
		echo ((empty($form_country)) ? 'Country<br>' : '' );
		echo ((empty($form_city)) ? 'City<br>' : '' );
		echo ((empty($form_district)) ? 'District<br>' : '' );
		echo '</fb:error>'; 
	}
?>
<fb:editor action="addclub.php?do-it" labelwidth="150">
  <fb:editor-text label="<?php echo $_ClubName; ?> Club of" name="club" value="<?php echo $form_club; ?>"/>
  <fb:editor-custom label="Country">
    <select name="country">
    <option value="0">Select Country:</option>
      <?php echo dropdown(countries(), $form_country); ?>
    </select>
  </fb:editor-custom>
  <fb:editor-text label="City (English Name)" name="city" value="<?php echo $form_city; ?>"/>
   <fb:editor-text label="District" name="district" value="<?php echo (($form_district==0) ? '' : $form_district); ?>"/>
  <fb:editor-text label="Homepage URL" name="url" value="<?php echo $form_url; ?>"/>
   <fb:editor_custom label="Facebook Group">
    <select name="groupid">
      <option value="0">Select your Club's Group:</option>
      <?php echo dropdown($groups, $form_groupid); ?>
       </select>
       <br><small>Note: you have to be a member of the group to add it.</small>
       <br><br><b><font color="#CC0000">Caution</font></b>: Before adding a new club make sure it doesn't exist yet in any form!
  </fb:editor_custom>
  <fb:editor-buttonset>
    <fb:editor-button value="Add" name="add" />
    <fb:editor-cancel href="<?php echo $appcallbackurl; ?>" />
  </fb:editor-buttonset>
</fb:editor>

<?
}

?>
<fb:google-analytics uacct="<?php echo $_google_analytics_account; ?>" />